import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { AlarmListComponent } from "./alarm-list/alarm-list.component";
import { FormBuilder, FormGroup, Validators, FormControl } from "@angular/forms";
import { MdDatepickerModule } from '@angular/material';
import { DatePipe } from "@angular/common";
import { List } from 'linqts';

import { ApplicationService } from "../../../service/application.service";
import { SafeResourceUrl, DomSanitizer } from "@angular/platform-browser";
import { Subscription } from "rxjs/Rx";

import { ConfirmationService, Message } from 'primeng/primeng';
import { FarmCategoryEnum } from '../../common/device-tag-selector/farm-category.enum';
import { DeviceCategoryEnum } from '../../common/device-tag-selector/device-category.enum';
import { Device } from "../../../entity/device.model";
import { DeviceGroupCategoryEnum } from '../../common/device-tag-selector/device-selector/device-selector.component';
@Component({
	selector: 'app-alarm-search',
	templateUrl: './alarm-search.component.html',
	styleUrls: ['./alarm-search.component.css']
})
export class AlarmSearchComponent implements OnInit, OnDestroy {
	public deviceCategories: DeviceCategoryEnum[] = [DeviceCategoryEnum.turbine, DeviceCategoryEnum.inverter];
	/**
	* 风机id集合
	* 
	* @type @memberof AlarmSearchComponent
	*/
	private turbineIds: number[] = [];

	/**
	* 查询时间formGroup；
	* 
	* @type @memberof AlarmSearchComponent
	*/
	@ViewChild(AlarmListComponent)
	alarmList: AlarmListComponent;

	public endTime = new Date();
	public startTime: Date = new Date(this.endTime.getFullYear(), this.endTime.getMonth(), this.endTime.getDate());
	private initDataComp: boolean = false;
	public subject: Subscription;


	/**
	 * 是否显示提示框
	 * 
	 * @type {number}
	 * @memberof AlarmSearchComponent
	 */
	display: boolean = false;
	mes: string;

	/**
	 * 查询时间间隔
	 * 
	 * @type {number}
	 * @memberof AlarmSearchComponent
	 */
	public timeInterval: number;
	/**
	 * 故障描述模糊查询
	 * 
	 * @memberof AlarmSearchComponent
	 */
	public divFaultDesc: string;
	/**
	 *风机最大选择数量
	 *
	 * @type {number}
	 * @memberof AlarmSearchComponent
	 */
	public deviceCountLimit: number;
	/**
   *刚进页面是默认选择风机数量
   *默认:null 不做限制
   * 0不选择
   * @memberof AlarmSearchComponent
   */
	defaultChooseDeviceCount;
	isClearSelectDevice = false;
	en: any;
	/**
	 *设备选择分组默认选中
	 *
	 * @type {DeviceGroupCategoryEnum}
	 * @memberof AlarmSearchComponent
	 */
	public deviceGroupCategory: DeviceGroupCategoryEnum = DeviceGroupCategoryEnum.byCircuit;
	constructor(
		private fb: FormBuilder,
		private datePipe: DatePipe,

		private application: ApplicationService,
		private sanitizer: DomSanitizer,
		private confirmation: ConfirmationService
	) { }


	ngOnInit(): void {
		this.en = {
			firstDayOfWeek: 0,
			dayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
			dayNamesShort: ["日", "一", "二", "三", "四", "五", "六"],
			dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
			monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
			monthNamesShort: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
			today: '今天',
			clear: '清除'
		};
		this.change();
		let subject = this.application.currentMenuParamsSubject.subscribe(
			next => {
				this.change();
			}
		)
		/* this.divFaultDesc.valueChanges
            .debounceTime(300)
            .distinctUntilChanged()
            .subscribe((form: any) => {}) */
	}
	ngOnDestroy(): void {
		if (this.subject) {
			this.subject.unsubscribe();
		}
	}
	private timer: any;
	/**
	* 得到选中风场风机集合
	* 
	* @type {Device[]}@memberof TurbineDataListComponent
	*/
	/* onFarmSelectedChange(ev: SelectorFarmsData[]) {
		this.turbineIds = [];
		let turbineArr = ev.map(o => o.turbine);
		turbineArr.forEach(o => {
			this.turbineIds.push(...o.map(k => k.turbineId));
		})
		this.alarmList.turbineIds = this.turbineIds;
	} */
	onSelectedDevicesChange(ev: Device[]) {

		this.turbineIds = [];
		this.turbineIds.push(...ev.map(o => o.deviceId));
		this.alarmList.turbineIds = this.turbineIds;
		this.alarmList.clear();
		// 广东代码
		/* clearTimeout(this.timer);
		this.timer = setTimeout(() => {
			this.initAlarmList();
		}, 1000); */

	}
	onCreateData(ev) {
		this.initDataComp = ev;
	}
	onSearch() {
		this.initAlarmList();
	}

	initAlarmList() {
		if (this.turbineIds.length <= 0) {
			alert("请选择风机");
			return;
		}
		if (this.endTime > new Date()) {
			this.mes = `结束时间不能大于当前时间`;
			return this.display = true;
		}
		if (this.startTime > this.endTime) {
			this.mes = `起始时间不能大于结束时间`;
			return this.display = true;
		}


		let time = this.endTime.getTime() - this.startTime.getTime() - 5000;    //毫秒数(5秒的延迟)
		let dayTime = time / 24 / 60 / 60 / 1000;             				    //天
		if (dayTime > this.timeInterval) {
			this.mes = `起始时间和结束时间不能超过${this.timeInterval}天`;
			return this.display = true;
		}
		this.alarmList.startTime = this.startTime;
		this.alarmList.endTime = this.endTime;
		this.alarmList.upData();
	}

	confirm(confirm: string) {
		this.confirmation.confirm({
			header: "提示信息",
			message: confirm,
			accept: () => {
				//Actual logic to perform a confirmation
			}
		});
	}
	change() {
		if (this.application.currentMenuParams && this.application.currentMenuParams["timeInterval"]) {
			this.timeInterval = Number(this.application.currentMenuParams["timeInterval"].paramValue);
		} else {
			this.timeInterval = null;
		}
		if (this.application.currentMenuParams && this.application.currentMenuParams['deviceCountLimit']) {
			this.deviceCountLimit = Number(this.application.currentMenuParams['deviceCountLimit'].paramValue);
		} else {
			this.deviceCountLimit = null;
		}
		if (this.application.currentMenuParams && this.application.currentMenuParams['defaultChooseDeviceCount']) {
			this.defaultChooseDeviceCount = Number(this.application.currentMenuParams['defaultChooseDeviceCount'].paramValue);
		} else {
			this.defaultChooseDeviceCount = null;
		}
		if (this.application.currentMenuParams && this.application.currentMenuParams['isClearSelectDevice']) {
			if (this.application.currentMenuParams['isClearSelectDevice'].paramValue == 'true') {
				this.isClearSelectDevice = true;
			}

		}

	}
}


// WEBPACK FOOTER //
// ./src/app/module/alarm/alarm-search/alarm-search.component.ts